package com.oraking.android.cmm.view;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.oraking.android.cmm.R;
import com.oraking.android.cmm.util.DisPlayMetricsManager;

/**
 * 파일명          : TextAlignView.java 
 * 작성일          : 2011. 9. 29.
 * 작성자          : <a href='http://oraking.springnote.com/' target='_blank'>Teddy</a>
 * Todo      : 양쪽정렬을 위한 TextView
 * 수정내용
 * -------------------------------------------------------------
 *      수정일      |      수정자      |              수정내용              
 * -------------------------------------------------------------
 *              |              |
 */

public class TextAlignView extends LinearLayout {

	private Context mContext;
	private LayoutInflater inflater;	// LayoutInflater - layout 을 위해 xml 에 기술된 데이터를 파싱해서 ViewGroup을 생성하는 역활.
	private int mTxtLen = 0;			// TextView에 넣는 글자 수
	
	public TextAlignView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		
	}
	
	public TextAlignView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.mContext = context;
	}
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.view.TextAlignView.init
	 * @writeDay : 2011. 9. 29. 오후 3:30:03
	 * @Todo  : TextView 초기화 하는 함수.
	 */
	private void init() {
		// TODO Auto-generated method stub
		int padding = DisPlayMetricsManager.getPixel(5);
		setBackgroundColor(getResources().getColor(R.color.form_label_background));
		setPadding(padding, 0, padding, 0);
		String str = (String)getTag();
		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(str!=null)
			setTextAlign(str.toCharArray());
	}

	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.view.TextAlignView.setTextAlign
	 * @writeDay : 2011. 9. 29. 오후 3:31:07
	 * @Todo  : Text를 양쪽정렬하는 Method
	 */
	private void setTextAlign(char[] charArray) {
		mTxtLen = charArray.length;
		if(mTxtLen==0) return;
		addView(getTextView(String.valueOf(charArray[0])));

		for(int i = 1; i < charArray.length; i ++){

			addView(getTextPaddingView());
			addView(getTextView(String.valueOf(charArray[i])));
		}
	}
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.view.TextAlignView.getTextView
	 * @writeDay : 2011. 9. 29. 오후 3:32:09
	 * @Todo  : TextView 가져오는 Method
	 */
	private TextView getTextView(String s) {
		// TODO Auto-generated method stub
		TextView textView = (TextView)inflater.inflate(R.layout.textalignview_textview, null);
		if(mTxtLen == 1){
			textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		} else{
			textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
		}
		textView.setTypeface(null, Typeface.BOLD);
		textView.setTypeface(Typeface.SERIF);
		textView.setText(s);
		return textView;
	}
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.view.TextAlignView.getTextPaddingView
	 * @writeDay : 2011. 9. 29. 오후 3:35:09
	 * @Todo  : TextView Padding 가져오는 Method
	 */
	private TextView getTextPaddingView(){
		TextView mPaddingTextView  = (TextView)inflater.inflate(R.layout.textalignview_padding, null);
		mPaddingTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, 1));
		return mPaddingTextView;
	}
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.cmm.view.TextAlignView.setText
	 * @writeDay : 2011. 9. 29. 오후 3:38:09
	 * @Todo  : TODO
	 */
	public void setText(String str){
		this.removeAllViews();
		setTag(str);
		init();
	}
	
}
